package com.whlilong.jxc.dialog;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.whlilong.jxc.db.DbManager;
import com.whlilong.jxc.model.Goodsbrand;
import com.whlilong.jxc.model.Goodsinfo;
import com.whlilong.jxc.model.Hsinfo;
import com.whlilong.jxc.model.Keshang;
import com.whlilong.jxc.model.Kstype;
import com.whlilong.jxc.sys.ExtentionInfo;

public class GoodsInfoSearchByConditon extends AbstractDialog {

	private List<Goodsinfo> list = new ArrayList<Goodsinfo>();
	private Text goodsIdText;
	private Text goodsNameText;
	private Text pymText;
	private Text goodsNoText;
	private Button selectAllCheck;
	private Button goodsIdCheck;
	private Button goodsNameCheck;
	private Button pymCheck;
	private Button goodsNoCheck;
	private Button goodsDistrictCheck;
	private Button goodsBrandCheck;
	private Button businessCheck;
	private Button goodsTypeCheck;
	private Combo goodsTypeCombo;
	private Combo businessCombo;
	private Combo goodsBrandCombo;
	private Combo goodsDistrictCombo;
	private String goodsid;
	private String goodsName;
	private String pym;
	private String goodsNo;
	private String goodsDistrict;
	private String goodsType;
	private String goodsBrand;
	private String business;

	public GoodsInfoSearchByConditon(Shell parentShell) {
		super(parentShell);
	}


	public Control createGroupText(Composite parent) {
		FormLayout fl_composite = new FormLayout();
		fl_composite.marginRight = 5;
		fl_composite.marginLeft = 10;
		fl_composite.marginTop = 20;
		parent.setLayout(fl_composite);

		Group group = new Group(parent, SWT.NONE);
		group.setText("条件查询");
		FormLayout fl_group = new FormLayout();
		fl_group.marginTop = 5;
		fl_group.marginRight = 5;
		fl_group.marginLeft = 5;
		group.setLayout(fl_group);
		FormData fd_group = new FormData();
		fd_group.top = new FormAttachment(0, -10);
		fd_group.left = new FormAttachment(10, -20);
		fd_group.right = new FormAttachment(90, 20);
		group.setLayoutData(fd_group);

		Composite composite = new Composite(group, SWT.NONE);
		GridLayout gl_composite = new GridLayout(6, false);
		gl_composite.verticalSpacing = 10;
		composite.setLayout(gl_composite);
		FormData fd_composite_1 = new FormData();
		fd_composite_1.bottom = new FormAttachment(90);
		fd_composite_1.left = new FormAttachment(0);
		composite.setLayoutData(fd_composite_1);

		Label goodsIdLabel = new Label(composite, SWT.NONE);
		goodsIdLabel.setAlignment(SWT.RIGHT);
		GridData gd_goodsIdLabel = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_goodsIdLabel.horizontalIndent = 5;
		gd_goodsIdLabel.widthHint = 75;
		goodsIdLabel.setLayoutData(gd_goodsIdLabel);
		goodsIdLabel.setText("商品编号：");

		goodsIdText = new Text(composite, SWT.BORDER);
		goodsIdText.setEnabled(false);
		GridData gd_goodsIdText = new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1);
		gd_goodsIdText.widthHint = 90;
		goodsIdText.setLayoutData(gd_goodsIdText);

		goodsIdCheck = new Button(composite, SWT.CHECK);

		GridData gd_goodsIdCheck = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_goodsIdCheck.widthHint = 35;
		goodsIdCheck.setLayoutData(gd_goodsIdCheck);

		Label goodsNameLabel = new Label(composite, SWT.NONE);
		goodsNameLabel.setAlignment(SWT.RIGHT);
		GridData gd_goodsNameLabel = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_goodsNameLabel.widthHint = 75;
		goodsNameLabel.setLayoutData(gd_goodsNameLabel);
		goodsNameLabel.setText("商品名称：");

		goodsNameText = new Text(composite, SWT.BORDER);
		goodsNameText.setEnabled(false);
		GridData gd_goodsNameText = new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1);
		gd_goodsNameText.widthHint = 90;
		goodsNameText.setLayoutData(gd_goodsNameText);

		goodsNameCheck = new Button(composite, SWT.CHECK);

		Label pymLabel = new Label(composite, SWT.NONE);
		pymLabel.setAlignment(SWT.RIGHT);
		GridData gd_pymLabel = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_pymLabel.widthHint = 75;
		pymLabel.setLayoutData(gd_pymLabel);
		pymLabel.setText("拼音码：");

		pymText = new Text(composite, SWT.BORDER);
		pymText.setEnabled(false);
		pymText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		pymCheck = new Button(composite, SWT.CHECK);

		Label goodsNoLabel = new Label(composite, SWT.NONE);
		goodsNoLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		goodsNoLabel.setText("商品货号：");

		goodsNoText = new Text(composite, SWT.BORDER);
		goodsNoText.setEnabled(false);
		goodsNoText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		goodsNoCheck = new Button(composite, SWT.CHECK);

		Label goodsDistrictLabel = new Label(composite, SWT.NONE);
		goodsDistrictLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		goodsDistrictLabel.setText("商品区域：");

		goodsDistrictCombo = new Combo(composite, SWT.READ_ONLY);
		goodsDistrictCombo.add("不定");
		List districtList = ExtentionInfo.getInstance().getKeshanginfo();
		for (int i = 0; i < districtList.size(); i++) {
			Keshang ks = (Keshang) districtList.get(i);
			goodsDistrictCombo.add(ks.getKeshangarea());
		}
		goodsDistrictCombo.select(0);
		goodsDistrictCombo.setEnabled(false);
		goodsDistrictCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				true, false, 1, 1));

		goodsDistrictCheck = new Button(composite, SWT.CHECK);

		Label goodsBrandLabel = new Label(composite, SWT.NONE);
		goodsBrandLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		goodsBrandLabel.setText("商品品牌：");

		goodsBrandCombo = new Combo(composite, SWT.READ_ONLY);
		goodsBrandCombo.add("不定");
		List brandList = ExtentionInfo.getInstance().getBrandInfo();
		for (int i = 0; i < brandList.size(); i++) {
			Goodsbrand ks = (Goodsbrand) brandList.get(i);
			goodsBrandCombo.add(ks.getGoodsbrandname());
		}
		goodsBrandCombo.select(0);
		goodsBrandCombo.setEnabled(false);
		goodsBrandCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		goodsBrandCheck = new Button(composite, SWT.CHECK);

		Label businessLabel = new Label(composite, SWT.NONE);
		businessLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		businessLabel.setText("供应商：");

		businessCombo = new Combo(composite, SWT.READ_ONLY);
		businessCombo.add("不定");
		List businessList = ExtentionInfo.getInstance().getBusinessInfoList();
		for (int i = 0; i < businessList.size(); i++) {
			Hsinfo ks = (Hsinfo) businessList.get(i);
			businessCombo.add(ks.getHsname());
		}
		businessCombo.select(0);
		businessCombo.setEnabled(false);
		businessCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		businessCheck = new Button(composite, SWT.CHECK);

		Label goodsTypeLabel = new Label(composite, SWT.NONE);
		goodsTypeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		goodsTypeLabel.setText("商品类别：");

		goodsTypeCombo = new Combo(composite, SWT.READ_ONLY);
		goodsTypeCombo.add("不定");
		List merchantTypeList = ExtentionInfo.getInstance().getKstypeinfo();
		for (int i = 0; i < merchantTypeList.size(); i++) {
			Kstype ks = (Kstype) merchantTypeList.get(i);
			goodsTypeCombo.add(ks.getKstype());
		}
		goodsTypeCombo.select(0);
		goodsTypeCombo.setEnabled(false);
		goodsTypeCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		goodsTypeCheck = new Button(composite, SWT.CHECK);

		selectAllCheck = new Button(parent, SWT.CHECK);

		selectAllCheck.setAlignment(SWT.RIGHT);
		FormData fd_selectAllCheck = new FormData();
		fd_selectAllCheck.top = new FormAttachment(group, 10);
		fd_selectAllCheck.bottom = new FormAttachment(100, -10);
		fd_selectAllCheck.right = new FormAttachment(100, -22);
		selectAllCheck.setLayoutData(fd_selectAllCheck);
		selectAllCheck.setText("全选(&A)");

		addListener();

		return composite;

	}

	private void addListener() {
		goodsIdCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (goodsIdCheck.getSelection()) {
					goodsIdText.setEnabled(true);
				} else {
					goodsIdText.setText("");
					goodsIdText.setEnabled(false);
				}
				update();
			}
		});
		goodsNameCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (goodsNameCheck.getSelection()) {
					goodsNameText.setEnabled(true);
				} else {
					goodsNameText.setText("");
					goodsNameText.setEnabled(false);
				}
				update();
			}
		});
		pymCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (pymCheck.getSelection()) {
					pymText.setEnabled(true);
				} else {
					pymText.setText("");
					pymText.setEnabled(false);
				}
				update();
			}
		});
		goodsNoCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (goodsNoCheck.getSelection()) {
					goodsNoText.setEnabled(true);
				} else {
					goodsNoText.setText("");
					goodsNoText.setEnabled(false);
				}
				update();
			}
		});
		goodsDistrictCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (goodsDistrictCheck.getSelection()) {
					goodsDistrictCombo.setEnabled(true);

				} else {
					goodsDistrictCombo.setText("");
					goodsDistrictCombo.setEnabled(false);
				}
				update();
			}
		});
		businessCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (businessCheck.getSelection()) {
					businessCombo.setEnabled(true);

				} else {
					businessCombo.setText("");
					businessCombo.setEnabled(false);
				}
				update();
			}
		});
		goodsBrandCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (goodsBrandCheck.getSelection()) {
					goodsBrandCombo.setEnabled(true);
				} else {
					goodsBrandCombo.setText("");
					goodsBrandCombo.setEnabled(false);
				}
				update();
			}
		});
		goodsTypeCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (goodsTypeCheck.getSelection()) {
					goodsTypeCombo.setEnabled(true);

				} else {
					goodsTypeCombo.setText("");
					goodsTypeCombo.setEnabled(false);
				}
				update();
			}
		});
		selectAllCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (selectAllCheck.getSelection()) {
					goodsIdCheck.setSelection(true);
					goodsNameCheck.setSelection(true);
					pymCheck.setSelection(true);
					goodsNoCheck.setSelection(true);
					goodsDistrictCheck.setSelection(true);
					goodsTypeCheck.setSelection(true);
					goodsBrandCheck.setSelection(true);
					businessCheck.setSelection(true);

				} else {
					goodsIdCheck.setSelection(false);
					goodsNameCheck.setSelection(false);
					pymCheck.setSelection(false);
					goodsNoCheck.setSelection(false);
					goodsDistrictCheck.setSelection(false);
					goodsTypeCheck.setSelection(false);
					goodsBrandCheck.setSelection(false);
					businessCheck.setSelection(false);
				}
				initial();
			}

		});

	}

	protected void initial() {
		if (goodsIdCheck.getSelection()) {
			goodsIdText.setEnabled(true);
		} else {
			goodsIdText.setText("");
			goodsIdText.setEnabled(false);
		}
		if (goodsNameCheck.getSelection()) {
			goodsNameText.setEnabled(true);
		} else {
			goodsNameText.setText("");
			goodsNameText.setEnabled(false);
		}
		if (pymCheck.getSelection()) {
			pymText.setEnabled(true);
		} else {
			pymText.setText("");
			pymText.setEnabled(false);
		}
		if (goodsNoCheck.getSelection()) {
			goodsNoText.setEnabled(true);
		} else {
			goodsNoText.setText("");
			goodsNoText.setEnabled(false);
		}
		if (goodsDistrictCheck.getSelection()) {
			goodsDistrictCombo.setEnabled(true);

		} else {
			goodsDistrictCombo.select(0);
			goodsDistrictCombo.setEnabled(false);
		}
		if (businessCheck.getSelection()) {
			businessCombo.setEnabled(true);

		} else {
			businessCombo.select(0);
			businessCombo.setEnabled(false);
		}
		if (goodsBrandCheck.getSelection()) {
			goodsBrandCombo.setEnabled(true);
		} else {
			goodsBrandCombo.select(0);
			goodsBrandCombo.setEnabled(false);
		}
		if (goodsTypeCheck.getSelection()) {
			goodsTypeCombo.setEnabled(true);

		} else {
			goodsTypeCombo.select(0);
			goodsTypeCombo.setEnabled(false);
		}
	}

	protected void update() {
		if (goodsIdCheck.getSelection() && goodsNameCheck.getSelection()
				&& pymCheck.getSelection() && goodsNoCheck.getSelection()
				&& goodsDistrictCheck.getSelection()
				&& goodsTypeCheck.getSelection()
				&& goodsBrandCheck.getSelection()
				&& businessCheck.getSelection()) {
			selectAllCheck.setSelection(true);
		} else {
			selectAllCheck.setSelection(false);
		}
	}
	
	@SuppressWarnings("unchecked")
	protected void exeSearch(){
		goodsid= goodsIdText.getText();
		goodsName = goodsNameText.getText();
		pym = pymText.getText();
		goodsNo = goodsNoText.getText();
		goodsDistrict = goodsDistrictCombo.getText();
		goodsType = goodsTypeCombo.getText();
		goodsBrand = goodsBrandCombo.getText();
		business = businessCombo.getText();
		
		StringBuffer hql = new StringBuffer("from Goodsinfo where 1=1");
		List paramlist = new ArrayList();
		List valuelist = new ArrayList();
		int count = 0;
		if(!goodsid.equals("")){
			hql.append(" and goodsid=?");
			paramlist.add(count);
			valuelist.add(goodsid);
			count++;
		}
		if(!goodsName.equals("")){
			hql.append(" and goodsname=?");
			paramlist.add(count);
			valuelist.add(goodsName);
			count++;
		}
		if(!pym.equals("")){
			hql.append(" and pym=?");
			paramlist.add(count);
			valuelist.add(pym.toUpperCase());
			count++;
		}
		if(!goodsNo.equals("")){
			hql.append(" and goodshh=?");
			paramlist.add(count);
			valuelist.add(goodsNo);
			count++;
		}
		if(goodsDistrictCheck.getSelection()){
			hql.append(" and address=?");
			paramlist.add(count);
			valuelist.add(goodsDistrict);
			count++;
		}
		if(goodsTypeCheck.getSelection()){
			hql.append(" and goodstpname=?");
			paramlist.add(count);
			valuelist.add(goodsType);
			count++;
		}
		if(goodsBrandCheck.getSelection()){
			hql.append(" and goodsbrandname=?");
			paramlist.add(count);
			valuelist.add(goodsBrand);
			count++;
		}
		if(businessCheck.getSelection()){
			hql.append(" and hsname=?");
			paramlist.add(count);
			valuelist.add(business);
			count++;
		}
		Integer[] param = new Integer[paramlist.size()];
		Object[] values = new Object[valuelist.size()];
		values = valuelist.toArray(values);
		param = (Integer[]) paramlist.toArray(param);
		list = DbManager.getInstance().search(hql.toString(),param, values);
		
	}

	public List<Goodsinfo> getList() {
		return list;
	}

	public void setList(List<Goodsinfo> list) {
		this.list = list;
	}
}
